package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysEmpRequest;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysEmpMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysEmp;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysEmpService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 员工信息业务层处理
 *
 * @author mars
 * @date 2024-03-16
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysEmpServiceImpl implements ISysEmpService {

    private final SysEmpMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysEmp add(SysEmpRequest request) {
        SysEmp entity = SysEmp.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysEmpRequest request) {
        SysEmp entity = SysEmp.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public SysEmp getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysEmp> pageList(SysEmpRequest request) {
        Page<SysEmp> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysEmp> query = this.buildWrapper(request);
        IPage<SysEmp> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysEmp> list(SysEmpRequest request) {
        LambdaQueryWrapper<SysEmp> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysEmp> buildWrapper(SysEmpRequest param) {
        LambdaQueryWrapper<SysEmp> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getName())){
               query.like(SysEmp::getName ,param.getName());
        }
         if (!ObjectUtils.isEmpty(param.getAge())){
               query.like(SysEmp::getAge ,param.getAge());
        }
         if (StringUtils.isNotBlank(param.getHobby())){
               query.like(SysEmp::getHobby ,param.getHobby());
        }
        return query;
    }

}
